﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Achievement.DataModels;
using Achievement.BLL.Provider;
using System.Web.Security;
using Ebooom.Leads.Business.AuthorRole;

namespace Achievement.Web.Common
{
    public static class UserInfo
    {
        public static Users CurrentUser
        {
            get
            {
                if (System.Web.HttpContext.Current.User != null)
                {
                    string username = System.Web.HttpContext.Current.User.Identity.Name;
                    if (!string.IsNullOrEmpty(username))
                    {
                        Users user = UsersProvider.Instance.GetUserByLoginName(System.Web.HttpContext.Current.User.Identity.Name);
                        return user;
                    }
                    else
                    {
                        //HttpContext.Current.Response.Redirect(FormsAuthentication.LoginUrl);
                        return null;
                    }
                }
                return null;
            }
        }

        public static bool ValidataRole(long role)
        {
            if (CurrentUser == null || !AuthorRoleUtils.IsHasRight((int)UserInfo.CurrentUser.Roles, role))
            {
                return false;
            }
            return true;
        }

        public static void AddCount()
        {
            if (UserInfo.CurrentUser != null)
            {
                if (HttpContext.Current.Application["UserCount"] == null)
                {
                    UserCount uc = new UserCount();
                    uc.UID = UserInfo.CurrentUser.ID;
                    uc.TimeOut = DateTime.Now;
                    Dictionary<long, UserCount> ucList = new Dictionary<long, UserCount>();
                    ucList.Add(uc.UID, uc);
                    HttpContext.Current.Application["UserCount"] = ucList;
                }
                else
                {
                    Dictionary<long, UserCount> ucList = (Dictionary<long, UserCount>)HttpContext.Current.Application["UserCount"];
                    if (ucList.ContainsKey(UserInfo.CurrentUser.ID))
                    {
                        UserCount uc = ucList[UserInfo.CurrentUser.ID];
                        uc.TimeOut = DateTime.Now;
                    }
                    else
                    {
                        UserCount uc = new UserCount();
                        uc.UID = UserInfo.CurrentUser.ID;
                        uc.TimeOut = DateTime.Now;
                        ucList.Add(uc.UID, uc);
                    }
                }
            }
        }

        public static long GetCount()
        {
            if (HttpContext.Current.Application["UserCount"] != null)
            {
                Dictionary<long, UserCount> ucList = (Dictionary<long, UserCount>)HttpContext.Current.Application["UserCount"];
                return ucList.Count;
            }
            return 0;
        }

        public static void ClearCount()
        { 
            if (HttpContext.Current.Application["UserCount"] != null)
            {
                Dictionary<long, UserCount> ucList = (Dictionary<long, UserCount>)HttpContext.Current.Application["UserCount"];
                foreach (var item in ucList)
                {
                    if (item.Value.TimeOut > DateTime.Now.AddMinutes(-20))
                    {
                        ucList.Remove(item.Key);
                    }
                }
            }
        }
    }
}